/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.runtime.module.jaas;

import org.mule.runtime.core.api.MuleEvent;
import org.mule.runtime.core.api.security.Authentication;
import org.mule.runtime.core.api.security.Credentials;

import java.util.Map;

import javax.security.auth.Subject;

public class JaasAuthentication implements Authentication {

  private boolean authenticated;
  private char[] credentials;
  private String user;
  private Map<String, Object> properties;
  private Subject subject;
  transient private MuleEvent event;

  public JaasAuthentication(Credentials credentials) {
    this.user = credentials.getUsername();
    this.credentials = credentials.getPassword();
  }

  public JaasAuthentication(Object user, Object credentials, Subject subject) {
    this.user = (String) user;
    this.credentials = ((String) credentials).toCharArray();
    this.subject = subject;
  }

  @Override
  public MuleEvent getEvent() {
    return event;
  }

  public void setEvent(MuleEvent muleEvent) {
    this.event = muleEvent;
  }

  @Override
  public void setAuthenticated(boolean b) {
    authenticated = b;
  }

  @Override
  public boolean isAuthenticated() {
    return authenticated;
  }

  @Override
  public Object getCredentials() {
    return new String(credentials);
  }

  @Override
  public Object getPrincipal() {
    return user;
  }

  @Override
  public Map<String, Object> getProperties() {
    return properties;
  }

  @Override
  public void setProperties(Map<String, Object> properties) {
    this.properties = properties;
  }

  public Subject getSubject() {
    return subject;
  }
}
